package config;

import com.fasterxml.jackson.annotation.JsonAutoDetect;
import com.fasterxml.jackson.annotation.PropertyAccessor;
import com.fasterxml.jackson.databind.ObjectMapper;
import message.MyListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.PropertySource;
import org.springframework.data.redis.connection.MessageListener;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.connection.jedis.JedisConnectionFactory;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.listener.PatternTopic;
import org.springframework.data.redis.listener.RedisMessageListenerContainer;
import org.springframework.data.redis.listener.Topic;
import org.springframework.data.redis.listener.adapter.MessageListenerAdapter;
import org.springframework.data.redis.repository.configuration.EnableRedisRepositories;
import org.springframework.data.redis.serializer.GenericJackson2JsonRedisSerializer;
import org.springframework.data.redis.serializer.Jackson2JsonRedisSerializer;
import org.springframework.data.redis.serializer.StringRedisSerializer;
import org.springframework.http.converter.json.Jackson2ObjectMapperBuilder;
import redis.clients.jedis.JedisPoolConfig;

import java.util.*;
import java.util.concurrent.CountDownLatch;

/**
 * Created by yanxi on 2018/4/15 0015.
 */
@Configuration
public class SpringMessageConfig {


    @Bean
    CountDownLatch latch(){
        return new CountDownLatch(1);
    }

    /**
     * 此处是为了测试方便
     * @param latch
     * @return
     */
    @Bean
    MyListener listener(CountDownLatch latch){
        return new MyListener(latch);
    }

    /**
     * 配置redis链接池
     * @return
     */
    @Bean
    public JedisPoolConfig jedisPoolConfig(){
        JedisPoolConfig jedisPoolConfig = new JedisPoolConfig();
        return jedisPoolConfig;
    }

    /**
     * 链接工厂
     * @return
     */
    @Bean
    public JedisConnectionFactory connectionFactory(){
        JedisConnectionFactory jedisConnectionFactory = new JedisConnectionFactory();
        jedisConnectionFactory.setDatabase(3);
        jedisConnectionFactory.setHostName("169.254.243.238");
        jedisConnectionFactory.setPort(6379);
        jedisConnectionFactory.setPoolConfig(jedisPoolConfig());
        return jedisConnectionFactory;
    }

    /**
     * 序列化实现 Redistemple 与MessageListenerAdapter 要用同样的序列化实现，不然反序列化会乱码
     * @return
     */
    @Bean
    public Jackson2JsonRedisSerializer<Object> jackson2JsonRedisSerializer() {
        final Jackson2JsonRedisSerializer<Object> jackson2JsonRedisSerializer = new Jackson2JsonRedisSerializer<>(Object.class);
        final ObjectMapper objectMapper = Jackson2ObjectMapperBuilder
                .json().build();
        objectMapper.setVisibility(PropertyAccessor.ALL, JsonAutoDetect.Visibility.ANY);
        objectMapper.enableDefaultTyping(ObjectMapper.DefaultTyping.NON_FINAL);
        jackson2JsonRedisSerializer.setObjectMapper(objectMapper);
        return jackson2JsonRedisSerializer;
    }

    /**
     *
     * @param connectionFactory
     * @param jackson2JsonRedisSerializer
     * @return
     */
    @Bean
    public RedisTemplate redisTemplate(@Autowired JedisConnectionFactory connectionFactory,@Autowired Jackson2JsonRedisSerializer jackson2JsonRedisSerializer){
        RedisTemplate redisTemplate = new RedisTemplate();
        redisTemplate.setConnectionFactory(connectionFactory);
        redisTemplate.setStringSerializer(new StringRedisSerializer());
        redisTemplate.setValueSerializer(jackson2JsonRedisSerializer);
        return redisTemplate;
    }

    /**
     * 订阅者适配器 可以不用实现MessageListener 来监听消息
     * @param listener
     * @param jackson2JsonRedisSerializer
     * @return
     */
    @Bean
    public MessageListenerAdapter messageListenerAdapter(@Autowired MyListener listener,@Autowired Jackson2JsonRedisSerializer jackson2JsonRedisSerializer){
        MessageListenerAdapter messageListenerAdapter = new MessageListenerAdapter();
        messageListenerAdapter.setDefaultListenerMethod("receiveMessage");
        messageListenerAdapter.setDelegate(listener);
        messageListenerAdapter.setSerializer(jackson2JsonRedisSerializer);
        messageListenerAdapter.setStringSerializer(new StringRedisSerializer());
        return messageListenerAdapter;
    }

    /**
     *
     * @param messageListenerAdapter
     * @return
     */
    @Bean
    public RedisMessageListenerContainer redisMessageListenerContainer(MessageListenerAdapter messageListenerAdapter){
        RedisMessageListenerContainer container = new RedisMessageListenerContainer();
        container.setTopicSerializer(new StringRedisSerializer());
        container.setConnectionFactory(connectionFactory());
        container.addMessageListener(messageListenerAdapter,new PatternTopic("redis"));

//        container.addMessageListener();
        Map<MessageListener,Set<Topic>> listenerSetMap = new HashMap<MessageListener,Set<Topic>>();
//        container.setMessageListeners(listenerSetMap);
        return container;
    }
}
